package com.academic.system.security;

import com.academic.system.model.User;
import com.academic.system.model.UserRole;
import com.academic.system.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 数据库初始化器
 * 在应用启动时自动创建管理员账号
 */
@Component
public class DatabaseInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否已存在管理员账号
        User admin = userRepository.findByUsername("admin");
        
        if (admin == null) {
            System.out.println("创建默认管理员账号...");
            
            // 创建管理员账号
            admin = new User();
            admin.setUsername("admin");
            admin.setPassword("admin123");
            admin.setRealName("系统管理员");
            admin.setRole(UserRole.ADMIN);
            admin.setDepartment("信息中心");
            admin.setEmail("admin@example.com");
            admin.setStatus(1);
            admin.setCreatedAt(LocalDateTime.now());
            
            userRepository.insert(admin);
            System.out.println("管理员账号创建成功");
            
            // 创建教师账号示例
            User teacher1 = new User();
            teacher1.setUsername("teacher1");
            teacher1.setPassword("teacher123");
            teacher1.setRealName("张教授");
            teacher1.setRole(UserRole.TEACHER);
            teacher1.setDepartment("计算机科学学院");
            teacher1.setEmail("teacher1@example.com");
            teacher1.setStatus(1);
            teacher1.setCreatedAt(LocalDateTime.now());
            teacher1.setCreator(admin);
            
            userRepository.insert(teacher1);
            
            User teacher2 = new User();
            teacher2.setUsername("teacher2");
            teacher2.setPassword("teacher123");
            teacher2.setRealName("王副教授");
            teacher2.setRole(UserRole.TEACHER);
            teacher2.setDepartment("电子工程学院");
            teacher2.setEmail("teacher2@example.com");
            teacher2.setStatus(1);
            teacher2.setCreatedAt(LocalDateTime.now());
            teacher2.setCreator(admin);
            
            userRepository.insert(teacher2);
            
            // 创建学生账号示例
            User student1 = new User();
            student1.setUsername("student1");
            student1.setPassword("student123");
            student1.setRealName("李同学");
            student1.setRole(UserRole.STUDENT);
            student1.setDepartment("计算机科学学院");
            student1.setEmail("student1@example.com");
            student1.setStatus(1);
            student1.setCreatedAt(LocalDateTime.now());
            student1.setCreator(teacher1);
            
            userRepository.insert(student1);
            
            User student2 = new User();
            student2.setUsername("student2");
            student2.setPassword("student123");
            student2.setRealName("赵同学");
            student2.setRole(UserRole.STUDENT);
            student2.setDepartment("电子工程学院");
            student2.setEmail("student2@example.com");
            student2.setStatus(1);
            student2.setCreatedAt(LocalDateTime.now());
            student2.setCreator(teacher2);
            
            userRepository.insert(student2);
            
            System.out.println("示例账号创建成功");
        } else {
            System.out.println("管理员账号已存在，跳过创建");
        }
    }
} 